package com.ishell.generate;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.ishell.generate.bean.DataTypeEnum;
import com.ishell.generate.util.ConvertUtil;
import com.ishell.generate.util.PropertyUtil;
import org.apache.commons.lang.StringUtils;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * @author: crazier.huang
 * @project: generate
 * @date: 2018/6/8
 * @desc: 类相关说明
 */
public class TableDesc {

    /**
     * 名称
     */
    private String name;
    /**
     * 注解
     */
    private String comment;

    private String priDataType;

    /**
     * 表中的列
     */
    private List<ColumnDesc> columns;




    public TableDesc() {
    }

    public TableDesc(String name) {
        this.name = name;
    }

    public TableDesc(Map<String,Object> rmap) {
        this.name = (String) rmap.get("TABLE_NAME");
        this.comment = (String) rmap.get("TABLE_COMMENT");
    }

    /**
     * 获取类名，去表前缀，转峰驼
     * @return
     */
    public String getClassName() {
        String className = StringUtils.stripStart(this.name,PropertyUtil.getValue("prex.table"));
        className = className.toLowerCase();
        className = ConvertUtil.upperCase(className);
        className = new String(ConvertUtil.camel(new StringBuffer(className)));
        return className;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public List<ColumnDesc> getColumns() {
        return columns;
    }

    public void setColumns(List<ColumnDesc> columns) {
        this.columns = columns;
    }

    public String getPriDataType() {
        for(ColumnDesc columnDesc : columns){
            if(StringUtils.equals(columnDesc.getAnnotation(),"@Id")){
                this.priDataType = DataTypeEnum.parseByType(columnDesc.getDataType()).getName();
                break;
            }
        }
        return this.priDataType;
    }
}
